home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7330 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: in-news.erinet.com!usenet
  2. From: blueice@erinet.com (blueice)
  3. Newsgroups: comp.lang.c
  4. Subject: Printing a histogram
  5. Date: Sun, 25 Feb 1996 17:19:30 GMT
  6. Organization: EriNet Online 513 436-9915
  7. Message-ID: <4gq5sa$1u8@eri2.erinet.com>
  8. NNTP-Posting-Host: edlp061.erinet.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I am new to programming. Currently I am working on a little
  12. programming problem that involves getting a list of numbers (with a ra
  13. nge of 0 to 100) from a user and then outputting a histogram which
  14. will print an asterick next to each range of numbers for every number
  15. falling in that range.
  16. For instance if a user enters 11,12,and 51 the histogram will look
  17. like:
  18.    00-09:
  19.    10-19:**
  20.    20-29:
  21.    30-39:
  22.    40-49:
  23.    50-59:*
  24.    60-69:
  25.    70-79:
  26.    80-89:
  27.    90-99:
  28.    100-:
  29.   
  30.  I was able to write up some pseudocode as follows:
  31. 1) Use a loop to get numbers from the user and store these numbers in
  32. an array.
  33. 2) Pass  that array to a function which loops through the array and
  34. uses 11 different IF statements to decide what range (i.e.
  35. 0-9,10-19,etc.) each number falls in. Each range has a variable
  36. assigned to it (using letters of the alphabet as variable names:
  37. a,b,c......up to k) and the value of that variable is incremented by
  38. one every time a number is found within the respective range. All
  39. variables should begin with a value of 0 obviously.
  40. 3) All eleven variables are then passed to another function which
  41. prints the ranges and an appropriate number of astericks next to each
  42. range. 11 different FOR loops are used (one for each variable) to
  43. print the proper number of astericks next to each range.
  44.  
  45.   I bravely coded this mess and it did work with some minor bugs. The
  46. code itself looked so horrible to me that I am embarrassed to post it.
  47. The problem is in using so many IF statements and FOR loops, the code
  48. looks way too messy and inefficient. I would appreciate any advice on
  49. how to code this problem more intelligently.  
  50.   Incidentally I am only learning to program as a hobby because I know
  51. that someone with my  lack of talent and ability (as evidenced by the
  52. back asswards way I am trying to write this code) could never hope to
  53. make money at programming. However, I would appreciate it if any of
  54. you pros could lower yourselves to help the mentally challenged.
  55. Thanks so much.
  56.  
  57.    
  58.  
  59. blueice@erinet.com
  60.  
  61. ***Please don't flame or I will melt!***
  62.  
  63.